﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using System.Text;
using com.loto.Common;

namespace com.huigo.Admin.Web.financial
{
    public partial class FeeFlowList : BasePage
    {
        #region 查询字段
        private string queryOrderNo//订单编号
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["orderNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["orderNo"]);
            }
        }
        private string queryCustName//经销商名字
        {
            get { return string.IsNullOrEmpty(Request.QueryString["CustName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["CustName"].ToString()); }
        }
        private DateTime queryStart//开始时间
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.MinValue;
            }
        }
        private DateTime queryEnd//结束时间
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.MinValue;
            }
        }
        private int queryFlowType//操作类型
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["flowType"],0);
            }
        }
        private string querypayNo//支付交易号
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["payNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["payNo"]);
            }
        }
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        private int queryOrderType//订单状态
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["OrderType"], 0);
            }
        }
        #endregion
        public decimal feeCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                Bind();

            }
        }
        private void Bind()
        {
            custName.Text = queryCustName;
            if (queryStart.ToString("yyyy-MM-dd") != string.Empty && queryEnd.ToString("yyyy-MM-dd") != string.Empty)
            {
                if (queryStart.ToString("yyyy-MM-dd") != "0001-01-01" && queryEnd.AddDays(1).ToString("yyyy-MM-dd") != "0001-01-02")
                {
                    beginDate.Text = queryStart.ToString("yyyy-MM-dd");
                    endDate.Text = queryEnd.ToString("yyyy-MM-dd");
                }
            }
            feeType.Text = queryFlowType.ToString();
            orderNO.Text = queryOrderNo;
            orderType.Text = queryOrderType.ToString();
            payNo.Text = querypayNo;
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("custName", queryCustName));
            argList.Add(new RequestFuncParams("orderNO", queryOrderNo));
            argList.Add(new RequestFuncParams("flowType", queryFlowType.ToString()));
            argList.Add(new RequestFuncParams("orderStatus", queryOrderType.ToString()));
            argList.Add(new RequestFuncParams("start", queryStart.ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("end", queryEnd.AddDays(1).ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("payNo", querypayNo));
            argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_CUST_FEE_FLOW_INFO_LIST, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                StringBuilder sb = new StringBuilder();
                sb.Append("<table><thead><tr><th>经销商</th><th>变动金额</th><th>支付交易号</th><th>消费时间</th><th>消费备注</th></tr></thead><tbody>");
                string content = "<tr><td>{0}</td><td>{1}</td><td/>{2}</td><td>{3}</td><td>{4}</td></tr>";
                for (int i = 0; i < parser.RowsCount; i++)
                {
                    feeCount += ConvertCurrencyPay(Convert.ToDecimal(parser["FlowChangeFee", i].ToString()));
                    sb.AppendFormat(content, parser["CustName", i].ToString(), ConvertCurrencyPay(Convert.ToDecimal(parser["FlowChangeFee", i].ToString())), parser["FlowPayNo", i].ToString(), Convert.ToDateTime(parser["FlowCreatedOn", i]).ToString("yyyy-MM-dd HH:mm"), parser["FlowMemo", i].ToString());
                }
                sb.Append("</tbody></table>");
                litFeeFlow.Text = sb.ToString();
            }
            else
                litFeeFlow.Text = string.Empty;
        }
        private void LoadData()
        {

            //充值方式
            List<DropList> statusDropList = Tools.DictToDropList(Dict.DictFeeType);
            if (statusDropList != null)
            {
                feeType.DataSource = statusDropList;
                feeType.DataValueField = "Id";
                feeType.DataTextField = "Value";
                feeType.DataBind();
            };
            //订单类型
                List<DropList> DropList = Tools.DictToDropList(Dict.DictInsType);
            if (statusDropList != null)
            {
                orderType.DataSource = DropList;
                orderType.DataValueField = "Id";
                orderType.DataTextField = "Value";
                orderType.DataBind();
            };
            
        }
        private string GetPagerUrl()
        {
            string url = "FeeFlowList.aspx?";
            url += "page={0}";
            if (queryCustName != string.Empty)
                url += "&CustName=" + queryCustName;
            if (queryStart != DateTime.MinValue)
                url += "&Start=" + queryStart.ToString("yyyy-MM-dd");
            if (queryEnd != DateTime.MinValue)
                url += "&End=" + queryEnd.ToString("yyyy-MM-dd");
            if (queryFlowType >0)
                url += "&flowType=" + queryFlowType;
            if (queryOrderType >0)
                url += "&OrderType=" + queryOrderType;
            if (queryOrderNo != string.Empty)
                url += "&orderNo=" + queryOrderNo;
            return url;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (orderNO.Text != string.Empty && orderType.SelectedValue == string.Empty)
            {
                MessageBox.Show(this.Page, "订单号和订单类型必填！");
                return;
            }
            string url = "FeeFlowList.aspx?";
            url += "page=1";
            if (custName.Text != string.Empty)
                url += "&CustName=" + custName.Text;
            if (beginDate.Text != string.Empty)
                url += "&Start=" + beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&End=" + endDate.Text;
            if (feeType.SelectedValue !=string.Empty)
                url += "&flowType=" + feeType.SelectedValue;
            if (orderType.SelectedValue != string.Empty)
                url += "&OrderType=" + orderType.SelectedValue;
            if (payNo.Text != string.Empty)
                url += "&payNo=" + payNo.Text;
            if (orderNO.Text != string.Empty)
                url += "&orderNo=" + orderNO.Text;
            Response.Redirect(url);
        }
    }
}